package com.limetalks.util;

import java.util.ArrayList;
import java.util.List;
import java.util.WeakHashMap;

import javax.servlet.http.HttpSession;

import com.limetalks.data.User;

public class SessionTracker {
	
	private static SessionTracker instance = new SessionTracker();
	private WeakHashMap<String, HttpSession> sessions = new WeakHashMap<String, HttpSession>();

	public static SessionTracker instance() {
		return instance;
	}

	private SessionTracker() {
	}

	public List<HttpSession> getSessions() {
		return new ArrayList<HttpSession>(sessions.values());
	}

	public void add(HttpSession session){
		sessions.put(session.getId(),session);
	}
	
	public void remove(HttpSession session){
		sessions.remove(session.getId());
	}
	
	public HttpSession getSession(String id){
		return sessions.get(id); 
	}
	
	public User getUserBySessionId(String sessionId){
		return (User)getSession(sessionId).getAttribute(GlobalStrings.SESSION_USER_PARAM);
	}
}
